-- ToME - Tales of Maj'Eyal
-- Copyright (C) 2009, 2010, 2011, 2012 Nicolas Casalini
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Nicolas Casalini "DarkGod"
-- darkgod@te4.org

require "engine.class"
require "engine.ui.Dialog"
local List = require "engine.ui.List"
local Map = require "engine.Map"

module(..., package.seeall, class.inherit(engine.ui.Dialog))

function _M:init(mx, my, tmx, tmy, extra)
	self.tmx, self.tmy = util.bound(tmx, 0, game.level.map.w - 1), util.bound(tmy, 0, game.level.map.h - 1)
	if tmx == game.player.x and tmy == game.player.y then self.on_player = true end

	self:generateList()
	if extra then table.insert(self.list, 1, {name=extra.name, action="extra", action_fct=extra.fct, color=extra.color}) end
	self.__showup = false

	local name = "Stances"
	local w = self.font_bold:size(name)
	engine.ui.Dialog.init(self, name, 1, 100, mx, my)

	local list = List.new{width=math.max(w, self.max) + 10, nb_items=math.min(15, #self.list), scrollbar=#self.list>15, list=self.list, fct=function(item) self:use(item) end, select=function(item) self:select(item) end}

	self:loadUI{
		{left=0, top=0, ui=list},
	}

	self:setupUI(true, true, function(w, h)
		self.force_x = mx - w / 2
		self.force_y = my - self.h + (self.ih + list.fh / 3)
		if self.force_y + h > game.h then self.force_y = game.h - h end
	end)

	self.mouse:reset()
	self.mouse:registerZone(0, 0, game.w, game.h, function(button, x, y, xrel, yrel, bx, by, event) if (button == "left" or button == "right") and event == "button" then self.key:triggerVirtual("EXIT") end end)
	self.mouse:registerZone(self.display_x, self.display_y, self.w, self.h, function(button, x, y, xrel, yrel, bx, by, event) if button == "right" and event == "button" then self.key:triggerVirtual("EXIT") else self:mouseEvent(button, x, y, xrel, yrel, bx, by, event) end end)
	self.key:addBinds{ EXIT = function() game:unregisterDialog(self) end, }
end

function _M:unload()
	engine.ui.Dialog.unload(self)
	game:targetMode(false, false)
	self.exited = true
end

function _M:use(item)
	if not item then return end
	game:unregisterDialog(self)
	game:targetMode(false, false)

	local act = item.action

	if act == "temple" then game.player:stancechange(1)
	elseif act == "rogue" then game.player:stancechange(2)
	elseif act == "shadow" then game.player:stancechange(3)
	elseif act == "death" then game.player:stancechange(4)
	elseif act == "chaos" then game.player:stancechange(5)
	end
end

local olditem = nil
function _M:select(item)
	if self.exited then return end
	if not item then return end
	if olditem and olditem == item then return end
	if not item.set_target then game:targetMode(false, false) return end

	game:targetMode(true, nil, nil, item.set_target)
	game.target:setSpot(self.tmx, self.tmy, "forced")
end

function _M:generateList()
	local list = {}
	local player = game.player

	-- Stances
	if game.player.stance[1].unlocked then list[#list+1] = {name="Temple Stance", action="temple", color=colors.simple(colors.WHITE)} end
	if game.player.stance[2].unlocked then list[#list+1] = {name="Rogue Stance", action="rogue", color=colors.simple(colors.UMBER)} end
	if game.player.stance[3].unlocked then list[#list+1] = {name="Shadow Stance", action="shadow", color=colors.simple(colors.DARK_BLUE)} end
	if game.player.stance[4].unlocked then list[#list+1] = {name="Death Stance", action="death", color=colors.simple(colors.BLACK)} end
	if game.player.stance[5].unlocked then list[#list+1] = {name="Chaos Stance", action="chaos", color=colors.simple(colors.PURPLE)} end
	
	self.max = 0
	self.maxh = 0
	for i, v in ipairs(list) do
		local w, h = self.font:size(v.name)
		self.max = math.max(self.max, w)
		self.maxh = self.maxh + h
	end

	self.list = list
end
